// 商户首页
var teacher = require('./model/TeacherModel.js')
var tripleDes = require('../../utils/CKCrypto.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    _currentTeacherId: '',
    _isCollection: false,
    teacherInfo: {},
    starLevel:0
  },
  headLoadError: function(event) {
    console.log("image load fail")
    this.setData({
      ['teacherInfo.teacherHead']: '../../images/icons/icon_teacher_1.png'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var id = tripleDes.deCode(options.id)
    if (id) {
      this.setData({
        _currentTeacherId: id
      })
      teacher.getTeacherInfo(id, function(data) {
        that.setData({
          teacherInfo: data,
          starLevel: Number(data.teacherRank),
          _isCollection: data.isCollection == '0' ? false : true
        })
      })
    }else{
      wx.navigateBack({
        
      })
    }
  },
  collectTeacher: function(event) {
    var that=this
    if(this.data._isCollection){
      //取消
      teacher.delCollectTeacher(this.data._currentTeacherId,function(){
        that.setData({
          _isCollection:false
        })
        wx.showToast({
          title: '已取消收藏',
          icon: null
        })
      })
    }else{
      //收藏
      teacher.collectTeacher(this.data._currentTeacherId,function(){
        that.setData({
          _isCollection: true
        })
        wx.showToast({
          title: '收藏成功',
          icon:null
        })
      })
    }
  },
  gotoWeb: function (event) {
    var that = this
    var teacherId = this.data._currentTeacherId;
    var teachername="";
    var phone = this.data.teacherInfo.contact
    wx.request({
      url: 'https://www.yuandingxiake.com/web/index.php?g=user&m=JK&a=webempty' , 
      method: 'POST', 
      header: {
        'content-type':'application/x-www-form-urlencoded'
      },
      data: {
        id: teacherId
      },
      success: function (data) {
       
        if (data.data != null && typeof (data.data) != undefined && data.data.length>0){
          teacher.getTeacherInfo(teacherId, function (data) { 
            teachername = data.teacherName;
            wx.navigateTo({
              url: '../website-official/website?teacherId=' + teacherId + '&teachername=' + teachername
            })
          })
        }
        else{
          wx.showToast({
            title: '该用户尚未开通微官网',
            icon: 'none'
          })
        }
      }
    })  
  },
  gotofx: function (event) {
    var that = this
    var teacherId = this.data._currentTeacherId;
    console.log("jiaoshi"+teacherId)
    wx.navigateTo({
      url: '../distribution/distribution?teacherId=' + teacherId
    })
  },
  courseItemClick: function(event) {
    var courseNo = event.currentTarget.dataset.courseno
    var position = event.currentTarget.dataset.position
    var title = event.currentTarget.dataset.title
    var _url = '../courseinfo/detail?courseId=' + courseNo + '&title=' + title
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log("监听用户下拉动作")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that=this
    var shareObject = {
      path: 'pages/teacher-home/teacher?id='+ that.options.id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          icon: ''
        })
      }
    }
    return shareObject;
  
  }
})